
from fontTools.pens.freetypePen import FreeTypePen
from fontTools.ttLib import TTFont
import os
import re
import json
import matplotlib.pyplot as plt
# 解析加密的font文件

def haveFornt(mon_list):
    font = TTFont('dc027189e0ba4cd.woff2')
    cmap_list = font.getBestCmap()

     # 创建英文转英文的字典
    dict_e_a = {'one':'1','two':'2','three':'3','four':'4','five':'5','six':'6',
                    'seven':'7','eight':'8','nine':'9','zero':'0'}
    # 将英文数据进行转换
    for i in cmap_list:
        for j in dict_e_a:
            if j == cmap_list[i]:
                cmap_list[i] = dict_e_a[j]
    # print('转换为阿拉伯数字的映射表为：',cmap_list)
    # 去掉加密的月票数据列表中的符号
    new_mon_list = mon_list
    
    # 最终解析月票数据
    for i in new_mon_list:
        for j in enumerate(i):
            for k in cmap_list:
                if j[1] == str(k):
                    i[j[0]] = cmap_list[k]
    print('解析之后的月票数据为:',new_mon_list)
     
    # 将字典转换成json格式
    json_data = json.dumps(new_mon_list ,ensure_ascii=False)+',\n'
    # 将数据保存到本地
    with open('翻页起小点月票榜数据爬取.json','a',encoding='utf-8')as f:
        f.write(json_data)

    # 将月票数据进行拼接
    # new_list = []
    # for i in new_mon_list:
    #     j = ''.join(i)
    #     new_list.append(j)
    # print('解析出的明文数据为：',new_list)
    # # 将名称和对应的月票数据放进字典，并转换成json格式及进行保存
    # for i in range(len(title_list)):
    #     dict_ = {}
    #     dict_[title_list[i]] = new_list[i]
    #     # 将字典转换成json格式
    #     json_data = json.dumps(dict_,ensure_ascii=False)+',\n'
    #     # 将数据保存到本地
    #     with open('翻页起小点月票榜数据爬取.json','a',encoding='utf-8')as f:
    #         f.write(json_data)
 
 
    # index = 1
    # for n, v in cmap.items():
    #     d = v
    #     glyph = font.getGlyphSet()[d]  # 通过字形名称选择某一字形对象
    #     pen = FreeTypePen(None)  # 实例化Pen子类
    #     print(pen)
    #     glyph.draw(pen)  # “画”出字形轮廓
    #     b = pen.array()
    #     print(index, '/', len(cmap), '~~~', d)
    #     # plt.figure()
    #     # plt.imshow(b)
    #     # plt.axis('off')  # 禁用坐标轴
    #     # os.makedirs('imgs', exist_ok=True)
    #     # plt.savefig('./imgs/{0}.jpg'.format(d))
    #     # # plt.show()    # 显示
    #     # plt.clf()
    #     # plt.cla()
    #     # plt.close()
    #     index += 1
 
 

 